<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>企业级流水线的两种部署场景 | 小阳爱技术</title>
    <meta name="generator" content="VuePress 1.9.5">
    <link rel="icon" href="/img/favicon.ico">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="Python, Linux, Nginx, Django, Flask, WEB, 前端, Java 全栈知识体系, java体系, java知识体系, java框架,java详解,java学习路线,java spring, java面试, 知识体系, java技术体系, java编程, java编程指南,java开发体系, java开发,java教程,java,java数据结构, 算法, 开发基础">
    <meta name="keywords" content="Python, Linux, Nginx, Django, Flask, WEB, 前端, Java 全栈知识体系, java体系, java知识体系, java框架,java详解,java学习路线,java spring, java面试, 知识体系, java技术体系, java编程, java编程指南,java开发体系, java开发,java教程,java,java数据结构, 算法, 开发基础">
    <meta name="theme-color" content="#11a8cd">
    <meta name="robots" content="all">
    <meta name="author" content="小阳爱技术">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.8cb71cba.css" as="style"><link rel="preload" href="/assets/js/app.9f33dad1.js" as="script"><link rel="preload" href="/assets/js/2.336839c5.js" as="script"><link rel="preload" href="/assets/js/3.00f81f09.js" as="script"><link rel="prefetch" href="/assets/js/10.78490e0e.js"><link rel="prefetch" href="/assets/js/11.358c0655.js"><link rel="prefetch" href="/assets/js/12.3d907ab6.js"><link rel="prefetch" href="/assets/js/13.e005782a.js"><link rel="prefetch" href="/assets/js/14.5c1831ed.js"><link rel="prefetch" href="/assets/js/15.3dae17f7.js"><link rel="prefetch" href="/assets/js/16.caf002bb.js"><link rel="prefetch" href="/assets/js/17.96d97c18.js"><link rel="prefetch" href="/assets/js/18.6f49a065.js"><link rel="prefetch" href="/assets/js/19.a72a1b44.js"><link rel="prefetch" href="/assets/js/20.5dbbd998.js"><link rel="prefetch" href="/assets/js/21.3b5c9825.js"><link rel="prefetch" href="/assets/js/22.588d1d79.js"><link rel="prefetch" href="/assets/js/23.bb2ba1d1.js"><link rel="prefetch" href="/assets/js/24.2036f1ef.js"><link rel="prefetch" href="/assets/js/25.7b40ded3.js"><link rel="prefetch" href="/assets/js/26.51a93ad7.js"><link rel="prefetch" href="/assets/js/27.23a56743.js"><link rel="prefetch" href="/assets/js/28.2bfb83a2.js"><link rel="prefetch" href="/assets/js/29.b8c6cf94.js"><link rel="prefetch" href="/assets/js/30.fe19a5d5.js"><link rel="prefetch" href="/assets/js/31.b68040bb.js"><link rel="prefetch" href="/assets/js/32.d9d3dba8.js"><link rel="prefetch" href="/assets/js/33.59561c54.js"><link rel="prefetch" href="/assets/js/34.243786a2.js"><link rel="prefetch" href="/assets/js/35.daf1b05e.js"><link rel="prefetch" href="/assets/js/36.13258281.js"><link rel="prefetch" href="/assets/js/37.4b591e31.js"><link rel="prefetch" href="/assets/js/38.13d6ce06.js"><link rel="prefetch" href="/assets/js/39.c51dd517.js"><link rel="prefetch" href="/assets/js/4.72262f85.js"><link rel="prefetch" href="/assets/js/40.227cf12d.js"><link rel="prefetch" href="/assets/js/41.8ada8611.js"><link rel="prefetch" href="/assets/js/42.b168f326.js"><link rel="prefetch" href="/assets/js/43.9df968fc.js"><link rel="prefetch" href="/assets/js/44.1546c9b1.js"><link rel="prefetch" href="/assets/js/45.c29bc1da.js"><link rel="prefetch" href="/assets/js/46.2de30989.js"><link rel="prefetch" href="/assets/js/47.34b5ca9a.js"><link rel="prefetch" href="/assets/js/5.56d0fbc7.js"><link rel="prefetch" href="/assets/js/6.b9ff2c2e.js"><link rel="prefetch" href="/assets/js/7.41c828d4.js"><link rel="prefetch" href="/assets/js/8.82412463.js"><link rel="prefetch" href="/assets/js/9.c499cdfa.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8cb71cba.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="小阳爱技术" class="logo"> <span class="site-name can-hide">小阳爱技术</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Python" class="dropdown-title"><a href="/python" class="link-title">Python</a> <span class="title" style="display:none;">Python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/894626/404" class="nav-link">暂无</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="DevOPs" class="dropdown-title"><a href="/devops/" class="link-title">DevOPs</a> <span class="title" style="display:none;">DevOPs</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Git</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Git核心命令</a></li></ul></li><li class="dropdown-item"><h4>GitLab</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Gitlab基础</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">CI/CD语法</a></li></ul></li><li class="dropdown-item"><h4>Jernkins</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins入门</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Pipeline流水线语法</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins集成各种工具</a></li></ul></li><li class="dropdown-item"><h4>企业级流水线案例</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">企业级流水线的两个场景</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Npm企业流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Java企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Python企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Golang企业级流水线案例</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/pages/beb6c0bd8a66cea6/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/beb6c0bd8a66cea6/" class="nav-link">网站</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com//" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="/img/logo.png"> <div class="blogger-info"><h3>小阳爱技术</h3> <span>数据库管理员 &amp;&amp; 容器云狂热分子</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Python" class="dropdown-title"><a href="/python" class="link-title">Python</a> <span class="title" style="display:none;">Python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/894626/404" class="nav-link">暂无</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="DevOPs" class="dropdown-title"><a href="/devops/" class="link-title">DevOPs</a> <span class="title" style="display:none;">DevOPs</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Git</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Git核心命令</a></li></ul></li><li class="dropdown-item"><h4>GitLab</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Gitlab基础</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">CI/CD语法</a></li></ul></li><li class="dropdown-item"><h4>Jernkins</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins入门</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Pipeline流水线语法</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins集成各种工具</a></li></ul></li><li class="dropdown-item"><h4>企业级流水线案例</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">企业级流水线的两个场景</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Npm企业流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Java企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Python企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Golang企业级流水线案例</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/pages/beb6c0bd8a66cea6/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/beb6c0bd8a66cea6/" class="nav-link">网站</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com//" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>git</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>gitlab</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>jenkins</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>企业级流水线</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/894626/" aria-current="page" class="active sidebar-link">企业级流水线的两种部署场景</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/894626/#普通虚拟机部署" class="sidebar-link">普通虚拟机部署</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/894626/#安装-jenkins-插件" class="sidebar-link">安装 Jenkins 插件</a></li><li class="sidebar-sub-header level3"><a href="/pages/894626/#配置-publish-over-ssh插件连接远程机器" class="sidebar-link">配置 Publish Over SSH 插件连接远程机器</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#创建流水线" class="sidebar-link">创建流水线</a></li><li class="sidebar-sub-header level3"><a href="/pages/894626/#部署结束发送邮件" class="sidebar-link">部署结束发送邮件</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/894626/#k8s容器化部署" class="sidebar-link">K8S容器化部署</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/894626/#安装-k8s-插件" class="sidebar-link">安装 k8s 插件</a></li><li class="sidebar-sub-header level3"><a href="/pages/894626/#jenkins-连接-k8s" class="sidebar-link">Jenkins 连接 K8S</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#第一步-创建admin-csr-json" class="sidebar-link">第一步：创建admin-csr.json</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#第二步-创建证书和私钥" class="sidebar-link">第二步：创建证书和私钥</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#第三步-配置证书" class="sidebar-link">第三步：配置证书</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#第四步-将证书私钥填写至jenkins" class="sidebar-link">第四步：将证书私钥填写至Jenkins</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#第五步-上传证书并输入密码" class="sidebar-link">第五步：上传证书并输入密码</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#jenkins-连接-k8s-2" class="sidebar-link">Jenkins 连接 k8s</a></li><li class="sidebar-sub-header level3"><a href="/pages/894626/#测试部署" class="sidebar-link">测试部署</a></li><li class="sidebar-sub-header level4"><a href="/pages/894626/#k8s-部署清单" class="sidebar-link">k8s 部署清单</a></li></ul></li></ul></li><li><a href="/pages/a9b729/" class="sidebar-link">npm企业级流水线</a></li><li><a href="/pages/7d05b5/" class="sidebar-link">java企业级流水线</a></li><li><a href="/pages/e2c1f7/" class="sidebar-link">Python企业级流水线</a></li><li><a href="/pages/16c5f8/" class="sidebar-link">Golang企业级流水线</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>自动化测试流水线</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=DevOps" title="分类" data-v-06225672>DevOps</a></li><li data-v-06225672><a href="/categories/?category=%E4%BC%81%E4%B8%9A%E7%BA%A7%E6%B5%81%E6%B0%B4%E7%BA%BF" title="分类" data-v-06225672>企业级流水线</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://blog.abck8s.com" target="_blank" title="作者" class="beLink" data-v-06225672>小阳爱技术</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2022-06-23</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">企业级流水线的两种部署场景<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="企业级流水线的两种部署场景"><a href="#企业级流水线的两种部署场景" class="header-anchor">#</a> 企业级流水线的两种部署场景</h1> <p>目前，在企业中部署应用一般就是普通虚拟机部署和k8s容器化部署两种方式。这篇文章主要用来介绍这两种场景下分别怎么部署。</p> <h2 id="普通虚拟机部署"><a href="#普通虚拟机部署" class="header-anchor">#</a> 普通虚拟机部署</h2> <p><img src="/img/image-20220623142426683.png" alt="image-20220623142426683"></p> <h3 id="安装-jenkins-插件"><a href="#安装-jenkins-插件" class="header-anchor">#</a> 安装 Jenkins 插件</h3> <p>普通机器部署一般是采用 <code>Publish Over SSH</code>插件。</p> <p>Jenkins-&gt;Manage Jenkins-&gt;Manage Plugins，点击 Available</p> <p><img src="/img/image-20220623142813412.png" alt="image-20220623142813412"></p> <h3 id="配置-publish-over-ssh插件连接远程机器"><a href="#配置-publish-over-ssh插件连接远程机器" class="header-anchor">#</a> 配置 <code>Publish Over SSH</code>插件连接远程机器</h3> <blockquote><p>注意：Jenkins使用 <code>Publish Over SSH</code> 部署时，需要将部署服务器设置 SSH 免密登录。</p></blockquote> <p><img src="/img/image-20220623143324172.png" alt="image-20220623143324172"></p> <p><img src="/img/image-20220623144016669.png" alt="image-20220623144016669"></p> <h4 id="创建流水线"><a href="#创建流水线" class="header-anchor">#</a> 创建流水线</h4> <p><img src="/img/image-20220623144951869.png" alt="image-20220623144951869"></p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline<span class="token punctuation">{</span>
    agent any
    stages<span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;拉取代码&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                <span class="token function">checkout</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">$</span><span class="token keyword">class</span><span class="token punctuation">:</span> <span class="token string">'GitSCM'</span><span class="token punctuation">,</span> branches<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>name<span class="token punctuation">:</span> <span class="token string">'*/main'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> extensions<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> userRemoteConfigs<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>credentialsId<span class="token punctuation">:</span> <span class="token string">'68a87abf-6af7-4e60-94dd-cf45c280c247'</span><span class="token punctuation">,</span> url<span class="token punctuation">:</span> <span class="token string">'git@gitlab.abck8s.com:xiaoyangaijishu/html.git'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;SonarQube代码审查&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                script <span class="token punctuation">{</span>
                    scannerHome <span class="token operator">=</span> tool <span class="token string">'sonarqube-scanner'</span>
                <span class="token punctuation">}</span>
            	<span class="token function">withSonarQubeEnv</span><span class="token punctuation">(</span><span class="token string">'sonarqube8.9.8'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                	sh <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">scannerHome</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/bin/sonar-scanner&quot;</span></span>
            	<span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;判断代码扫描状态&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        	steps <span class="token punctuation">{</span>
    			script <span class="token punctuation">{</span>
    			    <span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    				    qg <span class="token operator">=</span> <span class="token function">waitForQualityGate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        				<span class="token keyword">if</span> <span class="token punctuation">(</span>qg<span class="token punctuation">.</span>status <span class="token operator">!=</span> <span class="token string">'OK'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          					error <span class="token interpolation-string"><span class="token string">&quot;扫描失败，状态错误: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">qg<span class="token punctuation">.</span>status</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
                        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                            echo <span class="token interpolation-string"><span class="token string">&quot;扫描成功&quot;</span></span>
                        <span class="token punctuation">}</span>
    				<span class="token punctuation">}</span>
 			 	<span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><p><img src="/img/image-20220623145544448.png" alt="image-20220623145544448"></p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline<span class="token punctuation">{</span>
    agent any
    stages<span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;拉取代码&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                <span class="token function">checkout</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">$</span><span class="token keyword">class</span><span class="token punctuation">:</span> <span class="token string">'GitSCM'</span><span class="token punctuation">,</span> branches<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>name<span class="token punctuation">:</span> <span class="token string">'*/main'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> extensions<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> userRemoteConfigs<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>credentialsId<span class="token punctuation">:</span> <span class="token string">'68a87abf-6af7-4e60-94dd-cf45c280c247'</span><span class="token punctuation">,</span> url<span class="token punctuation">:</span> <span class="token string">'git@gitlab.abck8s.com:xiaoyangaijishu/html.git'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;SonarQube代码审查&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                script <span class="token punctuation">{</span>
                    scannerHome <span class="token operator">=</span> tool <span class="token string">'sonarqube-scanner'</span>
                <span class="token punctuation">}</span>
            	<span class="token function">withSonarQubeEnv</span><span class="token punctuation">(</span><span class="token string">'sonarqube8.9.8'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                	sh <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">scannerHome</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/bin/sonar-scanner&quot;</span></span>
            	<span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;判断代码扫描状态&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        	steps <span class="token punctuation">{</span>
    			script <span class="token punctuation">{</span>
    			    <span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    				    qg <span class="token operator">=</span> <span class="token function">waitForQualityGate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        				<span class="token keyword">if</span> <span class="token punctuation">(</span>qg<span class="token punctuation">.</span>status <span class="token operator">!=</span> <span class="token string">'OK'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          					error <span class="token interpolation-string"><span class="token string">&quot;扫描失败，状态错误: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">qg<span class="token punctuation">.</span>status</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
                        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                            echo <span class="token interpolation-string"><span class="token string">&quot;扫描成功&quot;</span></span>
                        <span class="token punctuation">}</span>
    				<span class="token punctuation">}</span>
 			 	<span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;部署代码&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        	steps <span class="token punctuation">{</span>
    			<span class="token function">sshPublisher</span><span class="token punctuation">(</span>
                    publishers<span class="token punctuation">:</span> <span class="token punctuation">[</span>
                        <span class="token function">sshPublisherDesc</span><span class="token punctuation">(</span>
                            configName<span class="token punctuation">:</span> <span class="token string">'web01'</span><span class="token punctuation">,</span> 
                            transfers<span class="token punctuation">:</span> <span class="token punctuation">[</span>
                                <span class="token function">sshTransfer</span><span class="token punctuation">(</span>
                                    cleanRemote<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    excludes<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
                                    execCommand<span class="token punctuation">:</span> <span class="token string">'''#!/bin/bash'''</span><span class="token punctuation">,</span> 
                                    execTimeout<span class="token punctuation">:</span> <span class="token number">120000</span><span class="token punctuation">,</span> 
                                    flatten<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    makeEmptyDirs<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    noDefaultExcludes<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    patternSeparator<span class="token punctuation">:</span> <span class="token string">'[, ]+'</span><span class="token punctuation">,</span> 
                                    remoteDirectory<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
                                    remoteDirectorySDF<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    removePrefix<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
                                    sourceFiles<span class="token punctuation">:</span> <span class="token string">'**/**'</span>
                                <span class="token punctuation">)</span>
                            <span class="token punctuation">]</span><span class="token punctuation">,</span> 
                            usePromotionTimestamp<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                            useWorkspaceInPromotion<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                            verbose<span class="token punctuation">:</span> <span class="token boolean">false</span>
                        <span class="token punctuation">)</span><span class="token punctuation">,</span> 
                        <span class="token function">sshPublisherDesc</span><span class="token punctuation">(</span>
                            configName<span class="token punctuation">:</span> <span class="token string">'web02'</span><span class="token punctuation">,</span> 
                            transfers<span class="token punctuation">:</span> <span class="token punctuation">[</span>
                                <span class="token function">sshTransfer</span><span class="token punctuation">(</span>
                                    cleanRemote<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    excludes<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
                                    execCommand<span class="token punctuation">:</span> <span class="token string">'#!/bin/bash'</span><span class="token punctuation">,</span> 
                                    execTimeout<span class="token punctuation">:</span> <span class="token number">120000</span><span class="token punctuation">,</span> 
                                    flatten<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    makeEmptyDirs<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    noDefaultExcludes<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    patternSeparator<span class="token punctuation">:</span> <span class="token string">'[, ]+'</span><span class="token punctuation">,</span> 
                                    remoteDirectory<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
                                    remoteDirectorySDF<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                                    removePrefix<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
                                    sourceFiles<span class="token punctuation">:</span> <span class="token string">'**/**'</span>
                                <span class="token punctuation">)</span>
                            <span class="token punctuation">]</span><span class="token punctuation">,</span> 
                            usePromotionTimestamp<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                            useWorkspaceInPromotion<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
                            verbose<span class="token punctuation">:</span> <span class="token boolean">false</span>
                        <span class="token punctuation">)</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br></div></div><p><img src="/img/image-20220623150117244.png" alt="image-20220623150117244"></p> <p><img src="/assets/img/image-20220623150153870.4e9fb081.png" alt="image-20220623150153870"></p> <p>部署成功。</p> <h3 id="部署结束发送邮件"><a href="#部署结束发送邮件" class="header-anchor">#</a> 部署结束发送邮件</h3> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>
post <span class="token punctuation">{</span>
  always <span class="token punctuation">{</span>
    emailext <span class="token punctuation">(</span>
    	body<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;&quot;&quot;
    	&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;meta charset=&quot;UTF-8&quot;&gt;&lt;title&gt;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">-第</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_NUMBER</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">次构建日志&lt;/title&gt;&lt;/head&gt;&lt;body leftmargin=&quot;8&quot; marginwidth=&quot;0&quot; topmargin=&quot;8&quot; marginheight=&quot;4&quot;
      offset=&quot;0&quot;&gt;&lt;table width=&quot;95%&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif&quot;&gt;&lt;tr&gt;(本邮件由系统自动发出，无需回复！)&lt;br/&gt;各位好，以下是</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">项目构建信息&lt;br/&gt;&lt;td&gt;&lt;font color=&quot;#CC0000&quot;&gt;构建结果 - </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_STATUS</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;br/&gt;&lt;b&gt;&lt;font color=&quot;#0B610B&quot;&gt;构建信息 - </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_STATUS</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/font&gt;&lt;/b&gt;&lt;hr size=&quot;2&quot; width=&quot;100%&quot; align=&quot;center&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;ul&gt;&lt;li&gt;项目名称：</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/li&gt;&lt;li&gt;构建编号：第</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_NUMBER</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">次构建&lt;/li&gt;&lt;li&gt;构建版本：2021.1.0.0&lt;/li&gt;&lt;li&gt;触发原因：</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">CAUSE</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/li&gt;&lt;li&gt;构建状态：</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_STATUS</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/li&gt;&lt;li&gt;构建日志：&lt;a href=&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">console&quot;&gt;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">console&lt;/a&gt;&lt;/li&gt;&lt;li&gt;构建地址：&lt;a href=&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;&gt;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/a&gt;&lt;/li&gt;&lt;li&gt;工作目录：&lt;a href=&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">ws&quot;&gt;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">ws&lt;/a&gt;&lt;/li&gt;&lt;li&gt;项目地址：&lt;a href=&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;&gt;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://1xx.1xx.3.xx:81xx/plateform-backend/report/interfaceReport/xxx/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">BUILD_TIMESTAMP</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/index.html&quot; &gt;查看测试报告&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;&lt;font color=&quot;#0B610B&quot;&gt;测试结果&lt;/font&gt;&lt;/h4&gt;&lt;hr size=&quot;2&quot; width=&quot;100%&quot; /&gt;&lt;div&gt;&lt;hr size=&quot;2&quot; width=&quot;100%&quot; /&gt;
</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$</span><span class="token expression">FAILED_TESTS</span></span><span class="token string">&lt;br/&gt;&lt;h4&gt;&lt;font color=&quot;#0B610B&quot;&gt;变更记录&lt;/font&gt;&lt;/h4&gt;&lt;hr size=&quot;2&quot; width=&quot;100%&quot; /&gt;变更明细：&lt;a href=&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">changes&quot;&gt;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">PROJECT_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">changes&lt;/a&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;
&quot;&quot;&quot;</span></span><span class="token punctuation">,</span>
    	mimeType<span class="token punctuation">:</span> <span class="token string">'text/html'</span><span class="token punctuation">,</span>
        subject<span class="token punctuation">:</span> <span class="token string">'自动化构建报告：$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!'</span><span class="token punctuation">,</span>
        to<span class="token punctuation">:</span> <span class="token string">'chenyangqit@163.com'</span>
    <span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="/img/image-20220623155216405.png" alt="image-20220623155216405"></p> <h2 id="k8s容器化部署"><a href="#k8s容器化部署" class="header-anchor">#</a> K8S容器化部署</h2> <p>K8S容器化部署就是说将应用部署到容器中。</p> <h3 id="安装-k8s-插件"><a href="#安装-k8s-插件" class="header-anchor">#</a> 安装 k8s 插件</h3> <p><img src="/assets/img/image-20220623155358667.ef4c9204.png" alt="image-20220623155358667"></p> <p><img src="/assets/img/image-20220623155840164.0bb2cac5.png" alt="image-20220623155840164"></p> <p><img src="/assets/img/image-20220623160612540.1aea94fd.png" alt="image-20220623160612540"></p> <h3 id="jenkins-连接-k8s"><a href="#jenkins-连接-k8s" class="header-anchor">#</a> Jenkins 连接 K8S</h3> <blockquote><p>最近我们构建和部署服务的方式与原来相比简直就是突飞猛进，像那种笨拙的、单一的、用于构建单体式应用程序的方式已经是过去式了。现在的应用为了提供更好的拓展性和可维护性，都会去拆解成各种相互依赖小、解耦性强的微服务，这些服务有各自的依赖和进度。这跟我们的Kubernetes不谋而合。</p> <p>使用Jenkins链接K8S需要创建一些秘钥。</p></blockquote> <h4 id="第一步-创建admin-csr-json"><a href="#第一步-创建admin-csr-json" class="header-anchor">#</a> 第一步：创建admin-csr.json</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">cat</span> <span class="token operator">&gt;</span> admin-csr.json <span class="token operator">&lt;&lt;</span> <span class="token string">EOF
{
    &quot;CN&quot;:&quot;admin&quot;,
    &quot;key&quot;:{
        &quot;algo&quot;:&quot;rsa&quot;,
        &quot;size&quot;:2048
    },
    &quot;names&quot;:[
        {
            &quot;C&quot;:&quot;CN&quot;,
            &quot;L&quot;:&quot;BeiJing&quot;,
            &quot;ST&quot;:&quot;BeiJing&quot;,
            &quot;O&quot;:&quot;system:masters&quot;,
            &quot;OU&quot;:&quot;System&quot;
        }
    ]
}
EOF</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h4 id="第二步-创建证书和私钥"><a href="#第二步-创建证书和私钥" class="header-anchor">#</a> 第二步：创建证书和私钥</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>cfssl gencert -ca<span class="token operator">=</span>/etc/kubernetes/pki/ca.crt -ca-key<span class="token operator">=</span>/etc/kubernetes/pki/ca.key --profile<span class="token operator">=</span>kubernetes admin-csr.json <span class="token operator">|</span> cfssljson -bare admin
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="第三步-配置证书"><a href="#第三步-配置证书" class="header-anchor">#</a> 第三步：配置证书</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>openssl pkcs12 -export -out ./jenkins-admin.pfx -inkey ./admin-key.pem -in ./admin.pem -passout pass:123456
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="第四步-将证书私钥填写至jenkins"><a href="#第四步-将证书私钥填写至jenkins" class="header-anchor">#</a> 第四步：将证书私钥填写至Jenkins</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">cat</span> /etc/kubernetes/pki/ca.crt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="/img/1341090-20190812121007110-882417879.png" alt="img"></p> <h4 id="第五步-上传证书并输入密码"><a href="#第五步-上传证书并输入密码" class="header-anchor">#</a> 第五步：上传证书并输入密码</h4> <p><img src="/img/image-20220623171359202.png" alt="image-20220623171359202"></p> <p><img src="/img/1341090-20190812121333313-1393693991.png" alt="img"></p> <p><img src="/img/1341090-20190812121500898-1766451213.png" alt="img"></p> <p><img src="/img/1341090-20190812121631813-541300395.png" alt="img"></p> <h4 id="jenkins-连接-k8s-2"><a href="#jenkins-连接-k8s-2" class="header-anchor">#</a> Jenkins 连接 k8s</h4> <p><img src="/img/image-20220623160807794.png" alt="image-20220623160807794"></p> <p><img src="/img/image-20220623170905294.png" alt="image-20220623170905294"></p> <p><img src="/img/image-20220623171002983.png" alt="image-20220623171002983"></p> <p><img src="/img/image-20220623171701988.png" alt="image-20220623171701988"></p> <h3 id="测试部署"><a href="#测试部署" class="header-anchor">#</a> 测试部署</h3> <p>使用 Jenkins 打包镜像部署到 k8s。</p> <blockquote><p>注意：配置清单中的变量居多，小心配置。</p></blockquote> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
  agent <span class="token punctuation">{</span>
    kubernetes <span class="token punctuation">{</span>
      cloud <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">KUBERNETES_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
      slaveConnectTimeout <span class="token number">1200</span>
      yaml <span class="token string">'''
apiVersion: v1
kind: Pod
spec:
  containers:
    - name: jnlp
      image: jenkins/inbound-agent:4.7-1-jdk11
      args: [\'$(JENKINS_SECRET)\', \'$(JENKINS_NAME)\']
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - mountPath: &quot;/etc/localtime&quot;
          name: &quot;volume-2&quot;
          readOnly: false
    - name: docker
      image: docker:19.03.15-git
      imagePullPolicy: IfNotPresent
      tty: true
      volumeMounts:
        - mountPath: &quot;/etc/localtime&quot;
          name: &quot;volume-2&quot;
          readOnly: false
        - mountPath: &quot;/var/run/docker.sock&quot;
          name: &quot;volume-docker&quot;
          readOnly: false
        - mountPath: &quot;/etc/hosts&quot;
          name: &quot;volume-hosts&quot;
          readOnly: false
    - name: kubectl
      image: registry.cn-beijing.aliyuncs.com/citools/kubectl:1.17.4
      imagePullPolicy: IfNotPresent
      tty: true
      command:
        - &quot;cat&quot;
      volumeMounts:
        - mountPath: &quot;/etc/localtime&quot;
          name: &quot;volume-2&quot;
          readOnly: false
        - mountPath: &quot;/var/run/docker.sock&quot;
          name: &quot;volume-docker&quot;
          readOnly: false
        - mountPath: &quot;/root/.kube&quot;
          name: &quot;kubeconfig&quot;
          readOnly: false
        - mountPath: &quot;/.kube&quot;
          name: &quot;kubeconfig&quot;
          readOnly: false
    - name: maven
      image: maven:3.6.3-openjdk-8
      tty: true
      command:
        - &quot;cat&quot;
      volumeMounts:
        - mountPath: &quot;/etc/localtime&quot;
          name: &quot;volume-2&quot;
          readOnly: false
        - mountPath: &quot;/root/.m2/repository&quot;
          name: &quot;volume-maven-repo&quot;
          readOnly: false
      env:
        - name: &quot;LANGUAGE&quot;
          value: &quot;en_US:en&quot;
        - name: &quot;LC_ALL&quot;
          value: &quot;en_US.UTF-8&quot;
        - name: &quot;LANG&quot;
          value: &quot;en_US.UTF-8&quot;
    - name: sonarscanner
      image: emeraldsquad/sonar-scanner:2.2.0
      tty: true
      command:
        - &quot;cat&quot;
      volumeMounts:
        - mountPath: &quot;/etc/localtime&quot;
          name: &quot;volume-2&quot;
          readOnly: false
        - mountPath: &quot;/root/.m2/repository&quot;
          name: &quot;volume-maven-repo&quot;
          readOnly: false
        - mountPath: &quot;/etc/hosts&quot;
          name: &quot;volume-hosts&quot;
          readOnly: false
      env:
        - name: &quot;LANGUAGE&quot;
          value: &quot;en_US:en&quot;
        - name: &quot;LC_ALL&quot;
          value: &quot;en_US.UTF-8&quot;
        - name: &quot;LANG&quot;
          value: &quot;en_US.UTF-8&quot;
  volumes:
    - name: volume-maven-repo
      emptyDir: {}
    - name: volume-2
      hostPath:
        path: &quot;/usr/share/zoneinfo/Asia/Shanghai&quot;
    - name: kubeconfig
      secret:
        secretName: kubeconfig
        items:
          - key: config
            path: config
    - name: volume-docker
      hostPath:
        path: &quot;/var/run/docker.sock&quot;
    - name: volume-hosts
      hostPath:
        path: /etc/hosts

'''</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  
  stages <span class="token punctuation">{</span>


    <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'源代码管理'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      parallel <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'拉取代码'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
                <span class="token function">git</span><span class="token punctuation">(</span>url<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">GIT_REPOSITORY_URL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">,</span> branch<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;master&quot;</span></span><span class="token punctuation">,</span> changelog<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> credentialsId<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">CREDENTIALS_ID</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'系统检查及初始化'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
            sh <span class="token interpolation-string"><span class="token string">&quot;&quot;&quot;

            echo &quot;Check Sysyem Env&quot;

&quot;&quot;&quot;</span></span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'代码处理'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      steps <span class="token punctuation">{</span>
        sh <span class="token interpolation-string"><span class="token string">&quot;&quot;&quot;
        
        git checkout </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$</span><span class="token expression">GIT_TAG</span></span><span class="token string">

        &quot;&quot;&quot;</span></span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
      <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;SonarQube代码审查&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
              <span class="token function">container</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string">'sonarscanner'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                script <span class="token punctuation">{</span>
                    scannerHome <span class="token operator">=</span> tool <span class="token string">'sonarqube-scanner'</span>
                <span class="token punctuation">}</span>
                <span class="token function">withSonarQubeEnv</span><span class="token punctuation">(</span><span class="token string">'sonarqube8.9.8'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    sh <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">scannerHome</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/bin/sonar-scanner&quot;</span></span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
          <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
      <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;判断代码扫描状态&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
              script <span class="token punctuation">{</span>
                  <span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                      qg <span class="token operator">=</span> <span class="token function">waitForQualityGate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                      <span class="token keyword">if</span> <span class="token punctuation">(</span>qg<span class="token punctuation">.</span>status <span class="token operator">!=</span> <span class="token string">'OK'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                          error <span class="token interpolation-string"><span class="token string">&quot;扫描失败，状态错误: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">qg<span class="token punctuation">.</span>status</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
                      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                          echo <span class="token interpolation-string"><span class="token string">&quot;扫描成功&quot;</span></span>
                      <span class="token punctuation">}</span>
                  <span class="token punctuation">}</span>
              <span class="token punctuation">}</span>
          <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>

    <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'编译及构建'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      parallel <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'初始化操作系统及kubernetes环境'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
            script <span class="token punctuation">{</span>
              CommitID <span class="token operator">=</span> <span class="token function">sh</span><span class="token punctuation">(</span>returnStdout<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> script<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;git log -n 1 --pretty=format:'%h'&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
              CommitMessage <span class="token operator">=</span> <span class="token function">sh</span><span class="token punctuation">(</span>returnStdout<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> script<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;git log -1 --pretty=format:'%h : %an  %s'&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
              <span class="token keyword">def</span> curDate <span class="token operator">=</span> <span class="token function">sh</span><span class="token punctuation">(</span>script<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;date '+%Y%m%d-%H%M%S'&quot;</span></span><span class="token punctuation">,</span> returnStdout<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
              TAG <span class="token operator">=</span> curDate<span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">..</span><span class="token number">14</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token interpolation-string"><span class="token string">&quot;-&quot;</span></span> <span class="token operator">+</span> CommitID <span class="token operator">+</span> <span class="token interpolation-string"><span class="token string">&quot;-master&quot;</span></span>
            <span class="token punctuation">}</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'构建镜像及检查kubernetes环境'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      parallel <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'构建镜像'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
            <span class="token function">withCredentials</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token function">usernamePassword</span><span class="token punctuation">(</span>credentialsId<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">DOCKER_REPOSITORY_CREDENTIAL_ID</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">,</span> passwordVariable<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;Password&quot;</span></span><span class="token punctuation">,</span> usernameVariable<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;Username&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token function">container</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string">'docker'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                sh <span class="token interpolation-string"><span class="token string">&quot;&quot;&quot;
                  docker build -t </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">HARBOR_HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">NAMESPACE_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">REPOSITORY_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">TAG</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> .
                  docker login </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">HARBOR_HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --username=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">Username</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --password=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">Password</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">
                  docker push </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">HARBOR_HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">NAMESPACE_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">REPOSITORY_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">TAG</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">
                &quot;&quot;&quot;</span></span>
              <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'检查kubernetes环境'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          steps <span class="token punctuation">{</span>
              <span class="token function">withCredentials</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token function">usernamePassword</span><span class="token punctuation">(</span>credentialsId<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">DOCKER_REPOSITORY_CREDENTIAL_ID</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">,</span> passwordVariable<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;Password&quot;</span></span><span class="token punctuation">,</span> usernameVariable<span class="token punctuation">:</span> <span class="token interpolation-string"><span class="token string">&quot;Username&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">container</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string">'kubectl'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                sh <span class="token interpolation-string"><span class="token string">&quot;&quot;&quot;
                    kubectl get nodes --kubeconfig=/root/.kube/config
                    kubectl delete secret aliyun-registry-key -n </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">K8S_NAMESPACE_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">
                    kubectl create secret docker-registry aliyun-registry-key -n </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">K8S_NAMESPACE_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --docker-server=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">HARBOR_HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --docker-username=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">Username</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --docker-password=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">Password</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --kubeconfig=/root/.kube/config
    &quot;&quot;&quot;</span></span>
                <span class="token punctuation">}</span>
              <span class="token punctuation">}</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'部署容器到kubernetes'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      steps <span class="token punctuation">{</span>
        <span class="token function">container</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string">'kubectl'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              sh <span class="token interpolation-string"><span class="token string">&quot;&quot;&quot;
               kubectl set image </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">CONTROLLER_TYPE</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> -l </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">CONTROLLER_LABEL</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> -n </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">K8S_NAMESPACE_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">CONTAINER_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">HARBOR_HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">NAMESPACE_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">REPOSITORY_NAME</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">TAG</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> --kubeconfig=/root/.kube/config
&quot;&quot;&quot;</span></span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
    post <span class="token punctuation">{</span>
  always <span class="token punctuation">{</span>
    emailext <span class="token punctuation">(</span>
    	body<span class="token punctuation">:</span> <span class="token string">'测试邮件'</span><span class="token punctuation">,</span>
    	mimeType<span class="token punctuation">:</span> <span class="token string">'text/html'</span><span class="token punctuation">,</span>
        subject<span class="token punctuation">:</span> <span class="token string">'自动化构建报告'</span><span class="token punctuation">,</span>
        to<span class="token punctuation">:</span> <span class="token string">'cxxxxit@163.com'</span>
    <span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br><span class="line-number">112</span><br><span class="line-number">113</span><br><span class="line-number">114</span><br><span class="line-number">115</span><br><span class="line-number">116</span><br><span class="line-number">117</span><br><span class="line-number">118</span><br><span class="line-number">119</span><br><span class="line-number">120</span><br><span class="line-number">121</span><br><span class="line-number">122</span><br><span class="line-number">123</span><br><span class="line-number">124</span><br><span class="line-number">125</span><br><span class="line-number">126</span><br><span class="line-number">127</span><br><span class="line-number">128</span><br><span class="line-number">129</span><br><span class="line-number">130</span><br><span class="line-number">131</span><br><span class="line-number">132</span><br><span class="line-number">133</span><br><span class="line-number">134</span><br><span class="line-number">135</span><br><span class="line-number">136</span><br><span class="line-number">137</span><br><span class="line-number">138</span><br><span class="line-number">139</span><br><span class="line-number">140</span><br><span class="line-number">141</span><br><span class="line-number">142</span><br><span class="line-number">143</span><br><span class="line-number">144</span><br><span class="line-number">145</span><br><span class="line-number">146</span><br><span class="line-number">147</span><br><span class="line-number">148</span><br><span class="line-number">149</span><br><span class="line-number">150</span><br><span class="line-number">151</span><br><span class="line-number">152</span><br><span class="line-number">153</span><br><span class="line-number">154</span><br><span class="line-number">155</span><br><span class="line-number">156</span><br><span class="line-number">157</span><br><span class="line-number">158</span><br><span class="line-number">159</span><br><span class="line-number">160</span><br><span class="line-number">161</span><br><span class="line-number">162</span><br><span class="line-number">163</span><br><span class="line-number">164</span><br><span class="line-number">165</span><br><span class="line-number">166</span><br><span class="line-number">167</span><br><span class="line-number">168</span><br><span class="line-number">169</span><br><span class="line-number">170</span><br><span class="line-number">171</span><br><span class="line-number">172</span><br><span class="line-number">173</span><br><span class="line-number">174</span><br><span class="line-number">175</span><br><span class="line-number">176</span><br><span class="line-number">177</span><br><span class="line-number">178</span><br><span class="line-number">179</span><br><span class="line-number">180</span><br><span class="line-number">181</span><br><span class="line-number">182</span><br><span class="line-number">183</span><br><span class="line-number">184</span><br><span class="line-number">185</span><br><span class="line-number">186</span><br><span class="line-number">187</span><br><span class="line-number">188</span><br><span class="line-number">189</span><br><span class="line-number">190</span><br><span class="line-number">191</span><br><span class="line-number">192</span><br><span class="line-number">193</span><br><span class="line-number">194</span><br><span class="line-number">195</span><br><span class="line-number">196</span><br><span class="line-number">197</span><br><span class="line-number">198</span><br><span class="line-number">199</span><br><span class="line-number">200</span><br><span class="line-number">201</span><br><span class="line-number">202</span><br><span class="line-number">203</span><br><span class="line-number">204</span><br><span class="line-number">205</span><br><span class="line-number">206</span><br><span class="line-number">207</span><br><span class="line-number">208</span><br><span class="line-number">209</span><br><span class="line-number">210</span><br><span class="line-number">211</span><br><span class="line-number">212</span><br><span class="line-number">213</span><br><span class="line-number">214</span><br><span class="line-number">215</span><br><span class="line-number">216</span><br><span class="line-number">217</span><br><span class="line-number">218</span><br><span class="line-number">219</span><br><span class="line-number">220</span><br><span class="line-number">221</span><br><span class="line-number">222</span><br><span class="line-number">223</span><br><span class="line-number">224</span><br><span class="line-number">225</span><br><span class="line-number">226</span><br><span class="line-number">227</span><br><span class="line-number">228</span><br><span class="line-number">229</span><br><span class="line-number">230</span><br><span class="line-number">231</span><br><span class="line-number">232</span><br><span class="line-number">233</span><br><span class="line-number">234</span><br><span class="line-number">235</span><br><span class="line-number">236</span><br><span class="line-number">237</span><br><span class="line-number">238</span><br><span class="line-number">239</span><br><span class="line-number">240</span><br><span class="line-number">241</span><br><span class="line-number">242</span><br><span class="line-number">243</span><br><span class="line-number">244</span><br><span class="line-number">245</span><br><span class="line-number">246</span><br></div></div><h4 id="k8s-部署清单"><a href="#k8s-部署清单" class="header-anchor">#</a> k8s 部署清单</h4> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> python
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> python
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> python
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> python
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> python
          <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx
<span class="token punctuation">---</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> python
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> python
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
      <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
      <span class="token key atrule">nodePort</span><span class="token punctuation">:</span> <span class="token number">30080</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> python
  <span class="token key atrule">type</span><span class="token punctuation">:</span> NodePort
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><p><img src="/img/image-20220623181307692.png" alt="image-20220623181307692"></p> <p><img src="/img/image-20220623195455031.png" alt="image-20220623195455031"></p></div></div>  <div class="page-edit"><div class="edit-link"><a href="https://github.com//edit/master/docs/05.DevOps/04.企业级流水线/00.企业级流水线的两种部署场景.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/06/24, 12:24:14</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/bc4b74/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Jenkins集成各种工具</div></a> <a href="/pages/a9b729/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">npm企业级流水线</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/bc4b74/" class="prev">Jenkins集成各种工具</a></span> <span class="next"><a href="/pages/a9b729/">npm企业级流水线</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/bc4b74/"><div>
            Jenkins集成各种工具
            <!----></div></a> <span class="date">06-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/c6f51e/"><div>
            Jenkins流水线语法
            <!----></div></a> <span class="date">06-23</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/30f9ff/"><div>
            01、Git
            <!----></div></a> <span class="date">06-22</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:chenyangqit@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://juejin.cn/user/730513894553032/course" title="掘金" target="_blank" class="iconfont icon-juejin"></a><a href="/img/weixin.jpg" title="微信" target="_blank" class="iconfont icon-weixin"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2019-2022
    <span>小阳爱技术 | <a href="https://blog.abck8s.com" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.9f33dad1.js" defer></script><script src="/assets/js/2.336839c5.js" defer></script><script src="/assets/js/3.00f81f09.js" defer></script>
  </body>
</html>
